<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // https://leetcode-cn.com/problems/ransom-note/
        // 高效率的方法还是考虑用哈希,这个方法效率低了
        // 还有数组 splice 的用法
        var canConstruct = function (ransomNote, magazine) {
            for(let i = 0; i < ransomNote.length; i++) {
                let index = magazine.indexOf(ransomNote.charAt(i));
                if (index != -1) {
                    // splice 是直接在数组上操作 返回的是切除的值，不能直接返回，加个中间变量
                    let arr = magazine.split('');
                    arr.splice(index, 1);
                    magazine = arr.join('');
                } else {
                    return false;
                }
            }
            return true;
        };

        console.log(canConstruct('aa', 'ab'));
    </script>
</body>

</html>